{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6ecd1c40-8127-4133-8e36-93cc3186870d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymssql"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "73530c34-9a58-49ce-953c-f9a8811363f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyodbc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c01a9c52-50d2-4ff9-9082-b3ad7ea06fac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pyodbc.drivers()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c7547512-c1be-450b-9122-e908048d2211",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2.0.40'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sqlalchemy\n",
    "\n",
    "sqlalchemy.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "57a42aaa-1766-48c6-9029-aca4aed7034b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sqlalchemy import create_engine\n",
    "\n",
    "engine = create_engine(\"sqlite+pysqlite:///:memory:\", echo=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "910517b1-65eb-4c94-8fe8-afa761e2226e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-06-24 09:43:17,819 INFO sqlalchemy.engine.Engine BEGIN (implicit)\n",
      "2025-06-24 09:43:17,820 INFO sqlalchemy.engine.Engine select 'hello world'\n",
      "2025-06-24 09:43:17,820 INFO sqlalchemy.engine.Engine [generated in 0.00138s] ()\n",
      "[('hello world',)]\n",
      "2025-06-24 09:43:17,821 INFO sqlalchemy.engine.Engine ROLLBACK\n"
     ]
    }
   ],
   "source": [
    "from sqlalchemy import text\n",
    "\n",
    "with engine.connect() as conn:\n",
    "    result = conn.execute(text(\"select 'hello world'\"))\n",
    "    print(result.all())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f18512b3-6935-495e-8e44-308268d33695",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-06-24 09:50:24,579 INFO sqlalchemy.engine.Engine BEGIN (implicit)\n",
      "2025-06-24 09:50:24,580 INFO sqlalchemy.engine.Engine CREATE TABLE some_table (x int, y int)\n",
      "2025-06-24 09:50:24,580 INFO sqlalchemy.engine.Engine [generated in 0.00091s] ()\n",
      "2025-06-24 09:50:24,581 INFO sqlalchemy.engine.Engine INSERT INTO some_table (x, y) VALUES (?, ?)\n",
      "2025-06-24 09:50:24,581 INFO sqlalchemy.engine.Engine [generated in 0.00026s] [(1, 1), (2, 4)]\n",
      "2025-06-24 09:50:24,581 INFO sqlalchemy.engine.Engine ROLLBACK\n"
     ]
    }
   ],
   "source": [
    "with engine.connect() as conn:\n",
    "    conn.execute(text(\"CREATE TABLE some_table (x int, y int)\"))\n",
    "    conn.execute(\n",
    "        text(\"INSERT INTO some_table (x, y) VALUES (:x, :y)\"),\n",
    "        [{\"x\": 1, \"y\": 1}, {\"x\": 2, \"y\": 4}],\n",
    "    )\n",
    "conn.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8ea07fd9-9795-476e-8009-025dda0a3ba3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-06-24 09:53:25,210 INFO sqlalchemy.engine.Engine BEGIN (implicit)\n",
      "2025-06-24 09:53:25,211 INFO sqlalchemy.engine.Engine SELECT x, y FROM some_table\n",
      "2025-06-24 09:53:25,212 INFO sqlalchemy.engine.Engine [cached since 130.8s ago] ()\n",
      "<sqlalchemy.engine.cursor.CursorResult object at 0x7f9490ca7f50>\n",
      "2025-06-24 09:53:25,212 INFO sqlalchemy.engine.Engine ROLLBACK\n"
     ]
    }
   ],
   "source": [
    "with engine.connect() as conn:\n",
    "    result = conn.execute(text(\"SELECT x, y FROM some_table\"))\n",
    "    print(result)\n",
    "    for row in result:\n",
    "        print(f\"x: {row.x}  y: {row.y}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "fc2ee945-c4f5-4aa9-a6b0-6aedb8469c4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sqlalchemy import Column, Integer, String, Table\n",
    "from sqlalchemy import MetaData\n",
    "metadata_obj = MetaData()\n",
    "user_table = Table(\n",
    "    \"user_account\",\n",
    "    metadata_obj,\n",
    "    Column(\"id\", Integer, primary_key=True),\n",
    "    Column(\"name\", String(30)),\n",
    "    Column(\"fullname\", String),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "52c5a5f0-e4e5-4f68-97a4-49e84708a2a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Column('name', String(length=30), table=<user_account>)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_table.c.name"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
